# -*- coding:utf-8 -*-
"""
作者：谷台阳
日期：2022年10月29日
"""
import cv2
import numpy as np
import math
import pandas as pd
from matplotlib import pyplot as plt
import time

data = pd.read_excel("clear.xlsx", sheet_name="clear", usecols=["Figure Name"])
y = []
start = time.perf_counter()
for t in range(1, 9951):
    num = int(data.loc[t - 1].values)
    image = cv2.imread('diff/diff{}.jpg'.format(num), cv2.IMREAD_GRAYSCALE)
    tmp = []
    for i in range(256):
        tmp.append(0)
    val = 0
    k = 0
    res = 0
    img = np.array(image)
    for i in range(len(img)):
        for j in range(len(img[i])):
            val = img[i][j]
            tmp[val] = float(tmp[val] + 1)
            k = float(k + 1)
    for i in range(len(tmp)):
        tmp[i] = float(tmp[i] / k)
    for i in range(len(tmp)):
        if tmp[i] == 0:
            res = res
        else:
            res = float(res - tmp[i] * (math.log(tmp[i]) / math.log(2.0)))
    # print(res)
    y.append(res)

    last = time.perf_counter() - start
    c = int(t / 9950 * 100)
    a = "*" * c
    b = "-" * (100 - c)
    print("\r{:^3.0f}%[{}->{}]{:.2f}s".format(c, a, b, last), end="")

frame = pd.DataFrame({"information": y})
frame.to_excel("information.xlsx", index=False, sheet_name="information")

x_axi = [i for i in range(0, 9950)]
plt.title("information")
plt.plot(x_axi, y, color="red", label="information")
plt.legend()
plt.show()